package cn.lysj.framework.common.validate.validate;

import cn.lysj.framework.common.validate.annotation.Email;
import cn.lysj.framework.common.validate.entity.Entity;
import java.util.regex.Pattern;

public class EmailValidate extends AbstractValidate<Email> {

    private final static Pattern EMAIL_PATTERN = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");

    public boolean validate(Entity entity) {
        if (null == entity.getValue()) return false;
        return EMAIL_PATTERN.matcher(String.valueOf(entity.getValue())).matches();
    }

    public void init() {
        this.setMsg(annotation.message());
        this.setGroups(annotation.groups());
    }

}
